/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.thinktank.controller;

import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.annotation.rest.AnonymousPostMapping;
import com.fzhucc.modules.security.domain.dto.AuthDto;
import com.fzhucc.modules.thinktank.domain.Company;
import com.fzhucc.modules.thinktank.domain.dto.*;
import com.fzhucc.modules.thinktank.service.CompanyService;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Woolsen
 * @date 2022-11-14
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "智库：上市公司管理")
@RequestMapping("/api/company")
public class CompanyController {

    private final CompanyService companyService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('company:list')")
    public void exportCompany(HttpServletResponse response, CompanyQueryCriteria criteria) throws IOException {
        companyService.download(companyService.queryAll(criteria), response);
    }

    @AnonymousGetMapping("id/{id}")
    @Log("查询单个上市公司")
    @ApiOperation("查询单个上市公司")
    public CompanyDto queryLeader(@PathVariable("id") Long id) {
        return companyService.findById(id);
    }

    @GetMapping
    @Log("查询上市公司")
    @ApiOperation("查询上市公司")
    /*@PreAuthorize("@el.check('company:list')")*/
    public ResponseEntity<Object> queryCompany(CompanyQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(false);
        return new ResponseEntity<>(companyService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @AnonymousGetMapping("/show")
    @Log("查询展示的上市公司")
    @ApiOperation("查询展示的上市公司")
    public ResponseEntity<Object> queryCompanyAnon(CompanyQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(true);
        criteria.setIsDelete(false);
        return new ResponseEntity<>(companyService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增上市公司")
    @ApiOperation("新增上市公司")
    @PreAuthorize("@el.check('company:add')")
    public ResponseEntity<Object> createCompany(@Validated @RequestBody Company resources) {
        return new ResponseEntity<>(companyService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改上市公司")
    @ApiOperation("修改上市公司")
    @PreAuthorize("@el.check('company:edit')")
    public ResponseEntity<Object> updateCompany(@Validated @RequestBody Company resources) {
        companyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除上市公司")
    @ApiOperation("删除上市公司")
    @PreAuthorize("@el.check('company:del')")
    public ResponseEntity<Object> deleteCompany(@RequestBody Long[] ids) {
        companyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("生成上市公司绑定小程序码")
    @Log("生成上市公司绑定小程序码")
    @PostMapping(value = "/generateQrCode")
    @PreAuthorize("@el.check('company:edit')")
    public AllBindDto generateQrCode(@RequestParam Long companyId) {
        return companyService.generateQrCode(companyId);
    }

    @ApiOperation("通过邀请码查询上市公司")
    @Log("通过邀请码查询上市公司")
    @AnonymousPostMapping(value = "/byCode")
    public CompanyDto findByCode(@RequestParam String code) {
        return companyService.findCompanyByCode(code);
    }

    @ApiOperation("二维码绑定上市公司")
    @Log("二维码绑定上市公司")
    @AnonymousPostMapping(value = "/bind")
    public AuthDto bind(@RequestBody AllBindDto2 dto, HttpServletRequest request) {
        return companyService.bindCompany(dto, request);
    }

    @Log("根据上市公司专家ListId 查询对应上市公司专家信息")
    @ApiOperation("根据上市公司专家ListId 查询对应上市公司专家信息")
    @PostMapping(value = "/getExpertInfoByListId")
    public ResponseEntity<Object> getExpertInfoByListId(@RequestBody Long[] expertIds) {
        return new ResponseEntity<>(companyService.getExpertInfoByListId(expertIds),HttpStatus.OK);
    }
}